package de.uni.muenster.idw2.location.bluetooth.service;

import java.util.ArrayList;
import java.util.List;

public class SynchroListUtils {
	
	public interface BluetoothDataFilter{
		boolean Filter(BluetoothDeviceData data);
	}
	
	//For copying the data.
	//Copies the elements starting by begin until end (inclusive)
	public static List<BluetoothDeviceData> doSynchronizedCopy(List<BluetoothDeviceData> synchroList, int begin, int end){
		return  doSynchronizedCopy(synchroList, begin, end, null);
	}
	
	public static List<BluetoothDeviceData> doSynchronizedCopy(List<BluetoothDeviceData> synchroList, int begin, int end, BluetoothDataFilter filter){
		List<BluetoothDeviceData> result = new ArrayList<BluetoothDeviceData>();
		synchronized(synchroList) {
			for (int i = begin; i<= end; i++) {
			  BluetoothDeviceData data = synchroList.get(i);
			  if (filter == null || filter.Filter(data))
			   result.add(new BluetoothDeviceData(data));
			}
		}
		return result;
	}
	
	//For copying the data. Other overloads for the function
	//Copies from a certain position until the end of the list;
	public static List<BluetoothDeviceData> doSynchronizedCopy(List<BluetoothDeviceData> synchroList, int begin){
		return doSynchronizedCopy(synchroList, begin, synchroList.size()-1, null);
	}
	
	public static List<BluetoothDeviceData> doSynchronizedCopy(List<BluetoothDeviceData> synchroList, int begin, BluetoothDataFilter filter){
		return doSynchronizedCopy(synchroList, begin, synchroList.size()-1, filter);
	}
	//For copying the data. Other overloads for the function.
	//This copies the whole function
	public static List<BluetoothDeviceData> doSynchronizedCopy(List<BluetoothDeviceData> synchroList){
		return doSynchronizedCopy(synchroList, 0, synchroList.size()-1, null);
	}
	
	public static List<BluetoothDeviceData> doSynchronizedCopy(List<BluetoothDeviceData> synchroList, BluetoothDataFilter filter){
		return doSynchronizedCopy(synchroList, 0, synchroList.size()-1, filter);
	}
	
	//Copy from one synchroList to another..
	public static void doSynchronizedCopyTo(List<BluetoothDeviceData> synchroList, List<BluetoothDeviceData> targetSynchroList, int begin, int end){
		synchronized (targetSynchroList) {
			targetSynchroList.clear();
			synchronized(synchroList) {
				for (int i = begin; i<= end; i++) {
				  BluetoothDeviceData data = synchroList.get(i);
				  targetSynchroList.add(new BluetoothDeviceData(data));
				}
			}
		}
	}
	
	//For copying the data. Other overloads for the function
	//Copies from a certain position until the end of the list;
	public static void doSynchronizedCopyTo(List<BluetoothDeviceData> synchroList, List<BluetoothDeviceData> targetSynchroList, int begin){
		doSynchronizedCopyTo(synchroList, targetSynchroList,  begin, synchroList.size()-1);
	}
	
	//For copying the data. Other overloads for the function.
	//This copies the whole function
	public static void doSynchronizedCopyTo(List<BluetoothDeviceData> synchroList, List<BluetoothDeviceData> targetSynchroList){
		doSynchronizedCopyTo(synchroList, targetSynchroList, 0, synchroList.size()-1);
	}
}
